package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.PlaceService;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class ShowEventsAdminServlet
 */
@WebServlet("/ShowEventsAdminServlet")
public class ShowEventsAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowEventsAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    /**
     * Method invoked when events are displayed for the admin
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
 
		HttpSession session = request.getSession(true);
		
		
		PlaceService service2 = new PlaceService();
		List<Place> places = new ArrayList<Place>();
		/**
		 * Get all places
		 */
		places = service2.getAllPlaces();
		
		List<Integer> idPlaces = new ArrayList<Integer>();
		List<String> namePlaces = new ArrayList<String>();
		
		
		for (int i = 0; i < places.size(); i++) {
			idPlaces.add(places.get(i).getId());
			namePlaces.add(places.get(i).getNamePlace());
		}
		
		/**
		 * Storing places for id and name
		 */
		Map<Integer, String> aux = new HashMap<Integer, String>();
		for (int i = 0; i < idPlaces.size(); i++) {
			aux.put(idPlaces.get(i), namePlaces.get(i));
		}
		
		request.setAttribute("placeMap", aux);
		session.setAttribute("placeMap", aux);
		
		
		EventService service3 = new EventService();
	
		List<Event> events = new ArrayList<Event>();
		/**
		 * Get all events
		 */
		events = service3.getAllEvents();
		
		session.setAttribute("eventsList", events);
		
		/**
	     * Invokes the action "updateeventsadmin.jsp"
	     */
		RequestDispatcher rd = request.getRequestDispatcher("updateeventsadmin.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked to get the id of an event by admin
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int idEvent = 0;
		response.setContentType("text/html");
        
		HttpSession session = request.getSession(true);
		
		/**
		 * Get the id of event
		 */
		idEvent = Integer.parseInt(request.getParameter("idEventAdmin"));
		session.setAttribute("idEventAdminSession", idEvent);
		
		/**
	     * Invokes the action "updateevent.jsp"
	     */
		response.sendRedirect("updateevent.jsp");	
		
	}

}
